#转换为关键帧
import av
import av.datasets


def ffmpeg_img_extract(videopath):
    container = av.open(videopath)
    
    stream = container.streams.video[0]
    stream.codec_context.skip_frame = 'NONKEY'
    
    for frame in container.decode(stream):
        #savepath = 'C:/Users/hp/Desktop/Audio_and_video_processing/Audio_and_video_demo/images/ffmpeg_img/' +'%d.jpg'%frame.index
        savepath = 'images/ffmpeg_img/' +'%d.jpg'%frame.index
        frame.to_image().save(savepath,quality=80)
 
def img_to_video(videopath):
    #转换为每帧

    container = av.open(videopath)
    
    for frame in container.decode(video=0):
        #savepath = 'C:/Users/hp/Desktop/Audio_and_video_processing/Audio_and_video_demo/images/ffmpeg_img/' +'%d.jpg'%frame.index
        savepath = 'images/ffmpeg_img/' +'%d.jpg'%frame.index
        frame.to_image().save(savepath)
    
    
    # #将mp4 转为 mkv
    # input_ = av.open('Never Give up.mp4')
    # output = av.open('Never Give up.mkv', 'w')
    
    # in_stream = input_.streams.video[0]
    # out_stream = output.add_stream(template=in_stream)
    
    # for packet in input_.demux(in_stream):
    #     if packet.dts is None:
    #         continue
    #     packet.stream = out_stream
    # output.mux(packet)

    # output.close()
